import { getBaseUrl } from './base-url'

// 是否启用本地登录
const LOCAL_LOGIN = false

/**
 * 跳转登录
 * @param {*} url 登录成功后跳转的url, 默认当前url
 */
export function autoLogin(
  url: string = window.location.pathname + window.location.search
) {
  const apiurl = getBaseUrl()
  const loginUrl = LOCAL_LOGIN ? '/login' : `${apiurl}/oauth/jump`
  // const loginUrl = LOCAL_LOGIN ? '/login' : `${apiurl}/sso/login`

  url = encodeURIComponent(url)
  window.location.href = `${loginUrl}?systemCode=${
    import.meta.env.VITE_OAUTH_SYSTEM_CODE
  }&returnUrl=${url}&time=${Date.now()}`
}

/**
 * 退出登录
 * @returns {object} 当以ajax方式退出时, 返回是否退出成功
 */
export async function logout() {
  let url = getBaseUrl()
  // clearLocalStorage()
  if (url) {
    localStorage.setItem('apiurl', url)
  }
  // url += '/slogout'
  url += `/oauth/logout?systemCode=${import.meta.env.VITE_OAUTH_SYSTEM_CODE}`

  // 本地登录模式
  if (LOCAL_LOGIN) {
    // await service.get(
    //   `/oauth/logout?systemCode=${import.meta.env.VITE_OAUTH_SYSTEM_CODE}`
    // )
    url = '/'
  }
  window.location.href = url
}
